var style = 'macarons2';
let rowSize = 4; //每行展示5个行业的指标趋势图
let totalCount = 100;
layui.use(['form', 'laydate'], function() {
	let laydate = layui.laydate;
	let form = layui.form;
	let today = new Date();
	let twoYearsAgo = new Date();
	twoYearsAgo.setFullYear(today.getFullYear() - 5);
	laydate.render({
		elem: '#startDate'
		, type: 'date'
		, format: 'yyyyMMdd'
		, value: twoYearsAgo
	});
	laydate.render({
		elem: '#endDate'
		, type: 'date'
		, format: 'yyyyMMdd'
		, value: today
	});
	dict2Select("stockIndustryZcfzIndicators", "indicatorField");
	dictToRadios("st","sortType","sortType","0");
	form.render();
	form.on('submit(stat)', function(formData) {
		var params = formData.field;
		render(params);
	});
	form.on('radio(sortType)',function(item){
		form.submit("iform",function(formData){
			var params = formData.field;
			render(params);
		});
	});
});

function dictToRadios(elementId,parentCode,itemName,checkedItem){
	var kvMap=dictionary(parentCode);
	var innerHtml="";
	kvMap.forEach(function(v,k,map){
		 var checked="";
			if(checkedItem!=null&&checkedItem==k){
				checked="checked";
            }
			innerHtml = innerHtml + '<input type="radio" name="'+itemName+'" lay-filter="'+itemName+'" title="'+v+'" value="'+k+'" '+checked+'>';
	});
	$("#"+elementId).html(innerHtml);
}

function render(params) {
	$("#container").html("");
	params.size = totalCount;
	params.sqlid='bi_industry_indicators_sum';
	params.tableName='d_stock_balancesheet';
	io.get("/stock/industry/indicators", function(res) {
		let d = res.data;
		let size = d.length;
		let rows = Math.ceil(size / rowSize);
		let rowHtml = "";
		for (let i = 0; i < rows; i++) {
			rowHtml += '<div class="layui-row">';
			for (let x = 0; x < rowSize; x++) {
				rowHtml += '<div class="layui-col-md3">' +
					'<div id="indicator' + (i * rowSize + x) + '" style="height:300px"></div>'
					+ '</div>';
			}
			+'</div>';
		}
		$("#container").html(rowHtml);
		renderData(d, translate("stockIndustryZcfzIndicators", params.indicatorField));
	}, params);
}

function renderData(data, iname) {
	let size = data.length;
	for (let i = 0; i < size; i++) {
		let d = data[i];
		renderLine(i,d, iname);
	}
}

function renderLine(i, d,iname) {
	var line = echarts.init(document.getElementById('indicator' + i), style);
	option = {
		title: {
			text: d.name,
			subtext: iname+" 趋势:"+numberFormat(d.linearScore,3),
			left: 'center'
		},
		tooltip: {
		    trigger: 'axis',
		    axisPointer: {
		      type: 'cross'
		    }
		},
		xAxis: {
			type: 'category',
			data: d.categories
		},
		yAxis: {
			type: 'value'
		},
		series: [
			{
				data: d.data,
				type: 'line',
      			stack: 'Total',
				label: {
			        show: true,
			        position: 'top'
			      },
		      	areaStyle: {},
		      	emphasis: {
		        	focus: 'series'
		      	},
			}
		]
	};
	line.setOption(option);
}
